'use strict';

const BaseController = require('./base');

class OrderController extends BaseController {
  get _service() {
    return 'order';
  }

  async _editStatus(status) {
    const { ctx } = this;
    const { id } = ctx.params();
    const result = await this._getService().update({ status, id });
    if (result) {
      this.success();
    } else {
      this.error('修改订单失败');
    }
  }

  async destroy() {
    await this._destory('删除账单失败');
  }

  async show() {
    const { ctx } = this;
    const { id } = ctx.params;
    const result = await this._getService().findOne({ id: +id });
    let persons = '';
    if (result.persons) {
      persons = await this.service.person.findAll({ ids: result.persons });
    }
    if (result) {
      this.success({ ...result, persons });
    } else {
      this.error('获取账单失败');
    }
  }

  // 获取所有账单
  async index() {
    const { ctx } = this;
    const { status, prop, sort, pageIndex, pageSize } = ctx.params();
    const { createdAt } = ctx.queries;
    const tempResult = await this._getService().findList({ status, prop, sort, pageIndex, pageSize, createdAt });
    const result = ctx.helper.flat(tempResult.data);
    if (result) {
      this.success(result, tempResult.count);
    } else {
      this.error('获取账单失败');
    }
  }

  // 获取登录人的发起的账单
  async indexMy() {
    const { ctx } = this;
    const { status, houseId, prop, sort, pageIndex, pageSize } = ctx.params();
    const tempResult = await this._getService().findList({ status, houseId, uid: ctx.userId, prop, sort, pageIndex, pageSize });
    const result = ctx.helper.flat(tempResult.data);
    if (result) {
      this.success(result, tempResult.count);
    } else {
      this.error('获取账单失败');
    }
  }

  // 获取登录人收到的租约
  async indexMyReview() {
    const { ctx } = this;
    const { status, houseId, prop, sort, pageIndex, pageSize } = ctx.params();
    const tempResult = await this._getService().findList({ status, houseId, oid: ctx.userId, prop, sort, pageIndex, pageSize });
    const result = ctx.helper.flat(tempResult.data);
    if (result) {
      this.success(result, tempResult.count);
    } else {
      this.error('获取账单失败');
    }
  }

  async agree() {
    const { ctx, app } = this;
    const { id } = ctx.params();
    const order = await this._getService().findOne({ id, isSimple: true });
    const { startTime, endTime, uid, houseId } = order;
    const result = await this._getService().update({ status: 2, id });
    // 拒绝该房屋时间段内所有的预约，并写明原因
    await this.service.appointment.updateOther({ startTime, endTime, status: [ 1 ], editStatus: 2, reason: '房子已被租走', houseId });
    // 创建租约
    await this.service.lease.create({ startTime, endTime, uid, houseId, status: 1 });
    // 重新启动判断租约的方法 判断当前租约的状态
    await app.runSchedule('lease');
    if (result) {
      this.success();
    } else {
      this.error('支付失败');
    }
  }

  async disagree() {
    const { ctx } = this;
    const { id } = ctx.params();
    const result = await this._getService().update({ status: 3, id });
    if (result) {
      this.success();
    } else {
      this.error('操作失败');
    }
  }

  // 是否支付过此房屋
  async isOrder() {
    const { ctx } = this;
    const { id } = ctx.params();
    const order = await this._getService().findOne({ houseId: id, uid: ctx.userId, status: 2, isSimple: true });
    if (order) {
      this.success({ flag: true });
    } else {
      this.success({ flag: false });
    }
  }

  // 获取房东每月的收入
  async statisticalIncome() {
    const { ctx } = this;
    const { startTime } = ctx.params();
    const result = await this._getService().statisticalIncome({ startTime, uid: ctx.userId });
    if (result && result[0]) {
      this.success(Object.entries(result[0]).map(item => ({ key: item[0] + '月', value: +item[1] })));
    } else {
      this.error('获取民宿失败');
    }
  }

  async statisticalIncomeAll() {
    const { ctx } = this;
    const { startTime } = ctx.params();
    const result = await this._getService().statisticalIncomeAll({ startTime, uid: ctx.userId });
    if (result && result[0]) {
      this.success(Object.entries(result[0]).map(item => ({ key: item[0] + '月', value: +item[1] })));
    } else {
      this.error('获取民宿失败');
    }
  }

  async tradingVolume() {
    const { ctx } = this;
    const { startTime, areaId } = ctx.params();
    const result = await this._getService().tradingVolume({ startTime, areaId });
    if (result && result[0]) {
      this.success(Object.entries(result[0]).map(item => ({ key: item[0] + '月', value: +item[1] })));
    } else {
      this.error('获取民宿失败');
    }
  }
}

module.exports = OrderController;
